package com.leigang.boot.redis.springbootredis.controller;

import com.leigang.boot.redis.springbootredis.service.IdempotencyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class IdemporencyController {
    @Autowired
    IdempotencyService idempotencyService;

    @GetMapping("/idemporency")
    public String idemporencyFunction() {
        String key = "uid:12011";
        Boolean flag = idempotencyService.lock(key, 10L, 1000L * 60);
        String msg = "";
        if (!flag) {
            // 获取锁失败
            System.err.println("获取锁失败");
            msg = "获取锁失败";
        } else {
            // 获取锁成功
            System.out.println("获取锁成功");
            msg = "获取锁成功";
        }
        // 释放锁
        idempotencyService.unLock(key);
        return msg;
    }
}
